import { proxy } from 'valtio'
import { get, post } from '../request'
export const UserFz = proxy({
    user: {},
    menus: [],
    name: '',
    data: [],
    username: '',
    password: '',
    list: [],
    status: '',
    current_page: { label: '工作台' },
    setCurrentPage: (current_page) => {
        UserFz.current_page = current_page
    },
    //登录
    login: async () => {
        const data = await post('/system/login', {
            username: UserFz.username,
            password: UserFz.password
        })
        console.log(data);
        
        UserFz.menus = data.data
        UserFz.user = data.list[0]
        UserFz.status = data.list[0].status

        if (UserFz.status){
          localStorage.setItem('user',UserFz.user._id)
        }
        return UserFz.status
    },
    // 获取用户列表
    getUserList: () => {
        get('/system/user', {
            name: UserFz.name
        }).then((res) => {
            UserFz.data = res.data
            UserFz.data.forEach((item) => {
                item.roleName = item.role.name
            })
        })
    },
    // 添加用户
    addUserList: (addData) => {
        console.log(addData)
        post('/system/register', addData)
    },
    // 删除用户
    deleteUserList: (id) => {
        post('/system/deleteUser', { id })
    },
    // 修改用户
    updateUserList: (updateData) => {
        post('/system/updateUser', updateData)
    },
    //搜索用户名和账号
    xgname: (name: string) => {
        UserFz.name = name
    },
    //修改输入名称
    xgsuername: (name: string) => {
        UserFz.username = name
    },
    // 修改输入密码
    xgpassword: (password: string) => {
        UserFz.password = password
    },
    onMenu:async () => {
      const token = localStorage.getItem('user')
      const {data}:{data:any} = await post('system/getMenu',{token});
      
      if (data){
        UserFz.menus = data.data
      }
    }
})
